package com.immunology.servlets;

import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.immunology.model.AttributeContent;
import com.immunology.model.MappingPages;
import com.immunology.model.MedicineCard;
import com.immunology.model.Survey;
import com.immunology.model.SurveyAnswer;
import com.immunology.model.Syndrome;
import com.immunology.model.User;
import com.immunology.service.SurveyAnswerService;
import com.immunology.service.SurveyService;
import com.immunology.service.SyndromeService;

public class AddSurveyToMedicineCardServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public AddSurveyToMedicineCardServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		MedicineCard medicineCard = (MedicineCard) getServletContext()
				.getAttribute(AttributeContent.MEDICINE_CARD);
		User doctor = (User) request.getSession().getAttribute(
				AttributeContent.USER);
		Survey survey = new Survey();
		survey.setDoctorId(doctor.getId());
		DateFormat day = new SimpleDateFormat("dd");
		DateFormat month = new SimpleDateFormat("MM");
		DateFormat year = new SimpleDateFormat("yyyy");
		java.util.Date date = new java.util.Date();
		Date surveyDate = new Date(1);
		surveyDate.setDate(Integer.parseInt(day.format(date)));
		surveyDate.setMonth(Integer.parseInt(month.format(date)) - 1);
		surveyDate.setYear(Integer.parseInt(year.format(date)) - 1900);
		survey.setDate(surveyDate);
		survey.setMedicineCardId(medicineCard.getId());
		survey.setDifficultyDegree(0);
		survey.setInsufficiencyDegree(0);
		SurveyService surveyService = SurveyService.getInstance();
		surveyService.insert(survey);

		int surveyId = surveyService.getLastSurveyId();
		SurveyAnswerService surveyAnswerService = SurveyAnswerService
				.getInstance();
		SurveyAnswer surveyAnswer = null;
		Enumeration<?> enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			String parameter = (String) enumeration.nextElement();
			System.out.println(parameter + " = "
					+ request.getParameter(parameter));
			if (!parameter.equals("save")
					&& !parameter.startsWith("description")) {
				int complaintId = Integer.parseInt(parameter);
				int value = Integer.parseInt(request.getParameter(parameter));
				surveyAnswer = new SurveyAnswer();
				surveyAnswer.setSurveyId(surveyId);
				surveyAnswer.setComplaintId(complaintId);
				surveyAnswer.setValue(value);
				String description = request.getParameter("description"
						+ complaintId);
				if ((description != null) && (description != "")) {
					surveyAnswer.setDescription(description);
				}
				surveyAnswerService.insert(surveyAnswer);
			}
		}
		Map<String, List<Integer>> surveyResultsMap = surveyService
				.getSurveyResultMap(surveyId);

		SyndromeService syndromeService = SyndromeService.getInstance();
		int syndromeId = (Integer) getServletContext().getAttribute(
				"syndromeId");
		Syndrome syndrome = syndromeService.getSyndromeById(syndromeId);
		String syndromeName = syndrome.getName();

		List<String> resultList = surveyAnswerService
				.getSumForComplaintTypeValue(surveyResultsMap);

		int difficultyDegree = Integer.valueOf(resultList.get(2));
		surveyService.setDifficultyDegreeBySurveyId(surveyId, difficultyDegree);

		request.setAttribute("syndromeName", syndromeName);
		request.setAttribute("surveyResultsMap", surveyResultsMap);
		request.setAttribute("resultList", resultList);
		getServletContext().getRequestDispatcher(
				MappingPages.SHOW_SURVEY_RESULTS_JSP)
				.forward(request, response);

	}
}
